package com.minazuki.signinserver.web;

import com.minazuki.signinserver.config.JwtProperties;
import com.minazuki.signinserver.contract.Contract;
import com.minazuki.signinserver.pojo.dao.Course;
import com.minazuki.signinserver.pojo.dao.CourseMapper;
import com.minazuki.signinserver.pojo.msg.Msg;
import com.minazuki.signinserver.service.CourseService;
import com.minazuki.signinserver.service.SigninService;
import com.minazuki.signinserver.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("course")
public class CourseController {
    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    CourseService cs;

    @Autowired
    SigninService ss;


    @Autowired
    CourseMapper courseMapper;
    @GetMapping("list")
    public ResponseEntity<Msg> getCourseList(@CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        List list = cs.getCourses(token);
        if (!list.isEmpty()) {
            msg.setCourses(list);
//            msg.setMsg(Contract.OP_SUCCESS_MESSAGE);
        } else msg.setMsg(Contract.LIST_EMPTY);
        return ResponseEntity.ok(msg);
    }

    @GetMapping("joinlist")
    public ResponseEntity<Msg> getCourseJoinList(@CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        List list = cs.getCoursesJoin(token);
        if (!list.isEmpty()) {
            msg.setCourses(list);
//            msg.setMsg(Contract.OP_SUCCESS_MESSAGE);
        } else msg.setMsg(Contract.LIST_EMPTY);
        return ResponseEntity.ok(msg);
    }

    @PostMapping("add")
    public ResponseEntity<Msg> addCourse(@Valid @RequestBody Course course, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        logger.info("add: " + JsonUtils.serialize(course));
        msg.setMsg(Contract.OP_SUCCESS_MESSAGE);
        cs.addCourse(course, token);
        return ResponseEntity.ok(msg);
    }

    @PostMapping(path = "update")
    public ResponseEntity<Msg> updateCourse(@Valid @RequestBody Course course, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        logger.info("update: " + JsonUtils.serialize(course));
        msg.setMsg(Contract.OP_FAIL_MESSAGE);
        msg.setMsg(Contract.OP_SUCCESS_MESSAGE);
        cs.updateCourse(course, token);
        return ResponseEntity.ok(msg);
    }

    @PostMapping("del/{id}")
    public ResponseEntity<Msg> delCourse(@PathVariable int id, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        if (cs.delCourse(id, token)) {
            msg.setMsg("删除成功");
        } else {
            msg.setMsg(Contract.OP_FAIL_MESSAGE);
        }
        return ResponseEntity.ok(msg);
    }

    @PostMapping("join/{cid}")
    public ResponseEntity<Msg> joinCourse(@PathVariable int cid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        if (cs.joinCourse(cid, token)) {
            msg.setMsg("加入成功");
        } else {
            msg.setMsg(Contract.OP_FAIL_MESSAGE);
        }
        return ResponseEntity.ok(msg);
    }

    @PostMapping("quit/{cid}")
    public ResponseEntity<Msg> quitCourse(@PathVariable int cid, @CookieValue(name = "SIGNSYS_TOKEN") String token) {
        Msg msg = new Msg();
        if (cs.quitCourse(cid, token)) {
            msg.setMsg("退出成功");
        } else {
            msg.setMsg(Contract.OP_FAIL_MESSAGE);
        }
        return ResponseEntity.ok(msg);
    }

    @GetMapping("name/{cid}")
    public ResponseEntity<String> queryCourseName(@PathVariable int cid) {
        String name = cs.queryCourseName(cid);
        return ResponseEntity.ok(name);
    }
    @GetMapping("test")
    public ResponseEntity<List<Course>> test() {
        return ResponseEntity.ok(courseMapper.test());
    }
}
